<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:vz="livcos.org/ns/doc/visualization"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns:v="livcos.org/ns/view"
  exclude-result-prefixes="v doc vz c f xhtml">
	
	<xsl:import href="/livcos.org/common/base.xsl"/>
	
	<xsl:template match="vz:table">
		<xsl:variable name="data" as="element()">
			<xsl:apply-templates select="vz:data"/>
		</xsl:variable>
		<div id="{@id}" class="block">
			<div style="display: inline-block;">
				<v:style ref="{resolve-uri('styles.css')}"/>
				<xsl:if test="normalize-space(vz:title) != ''">
					<div class="viz_table_title">
						<xsl:copy-of select="vz:title/node()"/>
					</div>
				</xsl:if>
				<xsl:apply-templates select="$data" mode="table_content">
					<xsl:with-param name="tbl" select="."/>
				</xsl:apply-templates>
				<xsl:if test="$data/@time or vz:note">
					<div class="viz_table_note">
						<xsl:value-of select="vz:note"/>
						<xsl:apply-templates select="$data" mode="table_data_info"/>
					</div>
				</xsl:if>
			</div>
		</div>
	</xsl:template>
	
	<xsl:template match="data" mode="table_data_info">
		<xsl:if test="@time">
			<xsl:variable name="tStr" select="format-dateTime(@time, '[D01].[M01].[Y0001] [H01]:[m01]:[s01]')"/>
				<xsl:choose>
					<xsl:when test="@refresh">
						<span class="viz_table_refresh"	title="Data fetched at {$tStr}. Click to refresh."
							onclick="refresh('{@refresh}', {{ref: '{@ref}'}})">
							<xsl:value-of select="$tStr"/>
						</span>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$tStr"/>
					</xsl:otherwise>
				</xsl:choose>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="data" mode="table_content">
		<xsl:param name="tbl"/>
		<xsl:variable name="rowsDef" select="$tbl/vz:rows"/>
		<xsl:variable name="nc" select="if ($rowsDef/@ns) then f:parse-ns($rowsDef/@ns) else $rowsDef"/>
		<xsl:variable name="_rows" select="if ($rowsDef/@select) then f:eval($rowsDef/@select, $nc) else */*"/>
		<xsl:variable name="def" as="element()*">
			<xsl:choose>
				<xsl:when test="$rowsDef">
					<xsl:sequence select="$rowsDef"/>
				</xsl:when>
				<xsl:when test="$tbl/vz:column">
					<rows xmlns="livcos.org/ns/doc/visualization">
						<xsl:sequence select="$tbl/vz:column"/>
					</rows>
				</xsl:when>
				<xsl:otherwise>
					<rows xmlns="livcos.org/ns/doc/visualization">
						<xsl:variable name="numCols" select="max($_rows/count(*))"/>
						<xsl:for-each select="($_rows[count(*) = $numCols])[1]/*">
							<column label="{local-name()}" select="{local-name()}"/>
						</xsl:for-each>
					</rows>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<div>
			<xsl:if test="count($_rows) > 32">
				<xsl:attribute name="style">height: 24em; overflow: auto;</xsl:attribute>
			</xsl:if>
			<table class="viz_table">
				<xsl:if test="$def/vz:column/@label">
					<thead>
						<xsl:copy-of select="$def/@id"/>
						<tr>
							<xsl:apply-templates select="$def/vz:column" mode="table_head"/>
						</tr>
					</thead>
				</xsl:if>
				<tbody id="{@ref}">
					<xsl:apply-templates select="$_rows" mode="table_body">
						<xsl:with-param name="def" select="$def"/>
					</xsl:apply-templates>
				</tbody>
			</table>
		</div>
	</xsl:template>
	
	<xsl:template match="vz:column" mode="table_head">
		<th>
			<xsl:copy-of select="@id"/>
			<xsl:copy-of select="@title"/>
			<xsl:value-of select="@label"/>
		</th>
	</xsl:template>
	
	<xsl:template match="*" mode="table_body">
		<xsl:param name="def"/>
		<xsl:variable name="row" select="."/>
		<xsl:variable name="style" select="if (position() mod 2 = 0) then 'odd' else 'even'"/>
		<xsl:variable name="subs" as="element()*">
			<xsl:for-each select="$def/vz:rows">
				<table>
					<thead>
						<xsl:copy-of select="@id"/>
						<tr>
							<xsl:apply-templates select="vz:column" mode="table_head"/>
						</tr>
					</thead>
					<tbody>
						<xsl:variable name="nc" select="if (@ns) then f:parse-ns(@ns) else ."/>
						<xsl:apply-templates select="if (@select) then f:eval($row, @select, $nc) else $row/*[*]" mode="table_body">
							<xsl:with-param name="def" select="."/>
						</xsl:apply-templates>
					</tbody>
				</table>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="hasSubs" select="$subs/xhtml:tbody/*[1]/*/text()"/>
		<tr class="{$style}">
			<xsl:if test="$hasSubs">
				<xsl:attribute name="onclick">var s = this.nextSibling.style; s.display = (s.display ==
					'none')?'':'none';</xsl:attribute>
				<xsl:attribute name="style">cursor: pointer;</xsl:attribute>
				<xsl:attribute name="class"><xsl:value-of select="$style"/> sub</xsl:attribute>
			</xsl:if>
			<xsl:for-each select="$def/vz:column">
				<td>
					<xsl:copy-of select="@align"/>
					<xsl:variable name="nc" select="if (@ns) then f:parse-ns(@ns) else ."/>
					<xsl:variable name="val">
						<xsl:choose>
							<xsl:when test="@select">
								<xsl:value-of select="f:eval($row, @select, $nc)"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:variable name="pos" select="position()"/>
								<xsl:value-of select="$row/*[$pos]"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:variable>
					<xsl:choose>
						<xsl:when test="not($val)">
							<xsl:text>&#160;</xsl:text>
						</xsl:when>
						<xsl:when test="@link">
							<xsl:variable name="lnk" select="f:eval($row, @link, $nc)"/>
							<xsl:choose>
								<xsl:when test="$lnk">
									<a href="{f:ref-to-uri(@view, $lnk, '.html')}">
										<xsl:value-of select="$val"/>
									</a>
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="$val"/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$val"/>
						</xsl:otherwise>
					</xsl:choose>
				</td>
			</xsl:for-each>
		</tr>
		<xsl:if test="$hasSubs">
			<tr style="display: none;">
				<td colspan="{count($def/vz:column)}" class="viz_table_inner">
					<xsl:sequence select="$subs"/>
				</td>
			</tr>
		</xsl:if>
	</xsl:template>
	
</xsl:stylesheet>
